package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.dao.core.BrandDiscountConfigDao;
import com.tbit.uqbike.client.dao.log.UserAuthLogDao;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.newEntity.AccountDiscount;
import com.tbit.uqbike.client.pojo.newEntity.UserAuthLog;
import com.tbit.uqbike.client.pojo.newEntity.UserDiscount;
import com.tbit.uqbike.client.service.FeeAppealService;
import com.tbit.uqbike.client.service.SpecialAuthService;
import com.tbit.uqbike.client.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * @Author: chen
 * @Description: 特殊认证
 * @Date: 创建于 11:05 2022/9/16
 */
@Service
public class SpecialAuthServiceImpl implements SpecialAuthService {

    @Autowired
    private BrandDiscountConfigDao brandDiscountConfigDao;
    @Autowired
    private UserService userService;

    @Autowired
    private UserAuthLogDao userAuthLogDao;
    @Autowired
    private FeeAppealService feeAppealService;

    @Override
    public List<AccountDiscount> getByAdAccountId(Integer adAccountId) {
        return brandDiscountConfigDao.getByAdAccountId(adAccountId);
    }

    @Override
    public AccountDiscount getDiscount(Integer adAccountId, User user) {

        UserDiscount userDiscounts = getByIdNo(user.getAccountId(), user.getIdNO());
        //UserDiscount userDiscounts =getByType(userId);
        if (userDiscounts == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("adAccountId", adAccountId);
        map.put("type", userDiscounts.getType());
        return brandDiscountConfigDao.getDiscount(map);
    }

    @Override
    public void auth(Integer userId, Integer type, String authNo, Integer adAccountId,
                     MultipartFile file, Integer accountId, String phone, String name) {
        List<AccountDiscount> accountDiscounts = getByAdAccountId(adAccountId);
        if (accountDiscounts.isEmpty()) {
            throw new BaseException("-200001", "该认证已经结束了");
        }
        User user = userService.getByUserId(userId);
        UserDiscount userDiscounts = getByIdNo(user.getAccountId(), user.getIdNO());
        if (userDiscounts != null) {
            throw new BaseException("-200002", "您已经认证过了,请勿重复认证");
        }
        UserAuthLog userAuthLogDB = getUserLog(userId, 0);
        if (userAuthLogDB != null) {
            throw new BaseException("-200005", "您已提交申请，请耐心等待审核");
        }
        UserAuthLog userAuthLog1 = userAuthLogDao.getByAuthNo(authNo);
        if (userAuthLog1 != null) {
            throw new BaseException("-200006", "该证件号已使用,请勿重复操作");
        }
        Integer configId = 0;
        for (AccountDiscount a : accountDiscounts) {
            if (Objects.equals(type, a.getType())) {
                configId = a.getConfigId();
            }
        }

        UserAuthLog userAuthLog = new UserAuthLog();
        userAuthLog.setAdAccountId(adAccountId);
        userAuthLog.setAddTime(new Date());
        userAuthLog.setAuthNo(authNo);
        userAuthLog.setConfigId(configId);
        userAuthLog.setState(0);
        userAuthLog.setUserId(userId);
        userAuthLog.setType(type);
        userAuthLog.setPhone(user.getPhone());
        userAuthLog.setAccountId(accountId);
        userAuthLog.setUserName(user.getName());
        /**文件名称*/
        String fileName = feeAppealService.ossFileUpload(file, accountId);
        userAuthLog.setUrl(fileName);
        userAuthLogDao.add(userAuthLog);
    }

    @Override
    public UserDiscount getUserDiscount(Integer userId, Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("type", type);
        return brandDiscountConfigDao.getUserDiscount(map);
    }

    @Override
    public UserDiscount getByType(Integer userId) {
        return brandDiscountConfigDao.getByType(userId);
    }

    @Override
    public UserAuthLog getUserLog(Integer userId, Integer state) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("state", state);
        return userAuthLogDao.getUserLog(map);
    }

    @Override
    public UserAuthLog getAllUserLog(Integer userId) {
        return userAuthLogDao.getAllUserLog(userId);
    }

    @Override
    public void insert(UserDiscount userDiscount) {
        userDiscount.setUpdateTime(new Date());
        brandDiscountConfigDao.add(userDiscount);
    }

    @Override
    public UserDiscount getByIdNo(Integer accountId, String idNO) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("idNO", idNO);
        return brandDiscountConfigDao.getByIdNo(map);
    }
}
